package com.eascs.p2p.investor.dubbo;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.p2p.common.entity.InvestorBankEntity;
import com.eascs.p2p.common.entity.InvestorEntity;
import com.eascs.p2p.common.investor.service.InvestorBankService;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.p2p.investor.dao.InvestorBankDao;
import com.eascs.p2p.investor.server.InvestorBankServiceInner;

@Service
public class InvestorBankDubboServiceImpl implements InvestorBankService {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private InvestorBankDao investorBankDao;
	@Autowired
	private InvestorBankServiceInner investorBankService;

	@Override
	public ModelResult<Boolean> insertInvestorBank(InvestorBankEntity investorBank) {
		ModelResult<Boolean> model = new ModelResult<Boolean>();
		try {
			int count = investorBankDao.insertInvestorBank(investorBank);
			model.setModel(false);
			if (count > 0) {
				model.setModel(true);
			}
		} catch (Exception e) {
			logger.error("新增投资者绑卡信息失败");
			model.withError("InvestorBankDubboServiceImpl.insertInvestorBank", "新增投资者绑卡信息失败");
			e.printStackTrace();
			return model;
		}
		return model;
	}

	@Override
	public ModelResult<Boolean> updateInvestorBank(InvestorBankEntity investorBank) {
		ModelResult<Boolean> model = new ModelResult<Boolean>();
		try {
			int count = investorBankDao.updateInvestorBank(investorBank);
			model.setModel(false);
			if (count > 0) {
				model.setModel(true);
			}
		} catch (Exception e) {
			logger.error("修改投资者绑卡信息失败");
			model.withError("InvestorBankDubboServiceImpl.updateInvestorBank", "修改投资者绑卡信息失败");
			e.printStackTrace();
			return model;
		}
		return model;
	}

	@Override
	public ModelResult<InvestorBankEntity> queryInvestorBankById(String id) {
		ModelResult<InvestorBankEntity> result = new ModelResult<>();
		try {
			InvestorBankEntity vo = investorBankDao.queryInvestorBankById(id);
			result.withModel(vo);
		} catch (Exception e) {
			logger.error("获取投资者绑卡信息失败, id:{}", id);
			result.withError("InvestorBankDubboServiceImpl.queryInvestorBankById", "获取投资者绑卡信息失败");
			e.printStackTrace();
			return result;
		}
		return result;
	}

	@Override
	public ModelResult<Boolean> investorBindBank(InvestorEntity investor, InvestorBankEntity investorBank) {
		ModelResult<Boolean> result = new ModelResult<>();
		try {
			return investorBankService.investorBindBank(investor, investorBank);
		} catch (Exception e) {
			logger.error("绑卡信息修改失败, account:{}, error:{}", investor.getAccount(), e.getMessage());
			result.withError("InvestorBankDubboServiceImpl.investorBindBank", "绑卡信息修改失败");
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public ModelResult<InvestorBankEntity> queryInvestorBankByInvestorId(String investorId) {
		ModelResult<InvestorBankEntity> result = new ModelResult<>();
		try {
			InvestorBankEntity vo = investorBankDao.queryInvestorBankByInvestorId(investorId);
			result.withModel(vo);
		} catch (Exception e) {
			logger.error("获取投资者绑卡信息失败, investorId:{}", investorId);
			result.withError("InvestorBankDubboServiceImpl.queryInvestorBankByInvestorId", "获取投资者绑卡信息失败");
			e.printStackTrace();
			return result;
		}
		return result;
	}

}
